package com.twb.core.base.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twb.core.base.entity.BaseEntity;
import com.twb.core.base.entity.BaseLoginUser;
import com.twb.core.base.mapper.TwbBaseMapper;
import com.twb.core.base.service.IBaseService;
import com.twb.core.config.TwbConfigProperties;
import com.twb.core.security.TwbSecurityUtil;

public class BaseService<M extends TwbBaseMapper<T>,T> extends ServiceImpl<M, T> implements IBaseService<T> {

	protected static Logger log = LoggerFactory.getLogger(BaseService.class);
	
	@Autowired
	HttpServletRequest request;
	
	@Autowired
	HttpServletResponse response;

	@Autowired
	protected TwbSecurityUtil twbSecurityUtil;
	
	@Autowired
	protected TwbConfigProperties twbConfigProperties;

	/**
	 * @Title  getLoginUser
	 * @Class: BaseController.java
	 * @Description 获取当登录人
	 * @param clazz
	 * @return
	 * @author DSY
	 * @date 2018年11月3日 下午8:23:10
	 */
	public <E extends BaseLoginUser> E getLoginUser() {
		return twbSecurityUtil.getLoginUser(request);
	}

	/**
	 * @Title  setCreatorInfo
	 * @Description 新增业务数据时，添加保存时间、保存人、版本号以及删除标识
	 * @param record
	 * @return
	 * @author DSY
	 * @date 2018年11月9日 下午6:16:50
	 */
	public <E extends BaseEntity> E setCreateInfo(E record) {
		BaseLoginUser loginUser = this.getLoginUser();
		if (loginUser != null) {
			record.setCreator(loginUser.getName());
			record.setCreatorId(loginUser.getId());
			record.setCreatorOrgid(loginUser.getOrgId());
		} else {
			record.setUpdater("Not Login User");
			log.error("用户信息不存在");
		}
		record.setCreateDate(LocalDateTime.now());
//		record.setVersion(1L);
		record.setIsdel(0);
		return record;
	}

	/**
	 * @Title  setUpdateInfo
	 * @Description 更新业务数据时，添加更新人、更新时间信息
	 * @param record
	 * @return
	 * @author DSY
	 * @date 2018年11月9日 下午6:16:22
	 */
	public <E extends BaseEntity> E setUpdateInfo(E record) {
		BaseLoginUser loginUser = this.getLoginUser();
		if (loginUser != null) {
			record.setUpdater(loginUser.getName());
			record.setUpdaterId(loginUser.getId());
		} else {
			record.setUpdater("Not Login User");
			log.error("用户信息不存在");
		}
		record.setUpdateDate(LocalDateTime.now());
		return record;
	}

	public String getUUID() {
		return UUID.randomUUID().toString().replace("-", "");
	}

	@Override
	public boolean remove(T record) {
		Wrapper<T> queryWrapper = new QueryWrapper<T>(record);
		return this.remove(queryWrapper);
	}

	@Override
	public long count(T record) {
		Wrapper<T> queryWrapper = new QueryWrapper<T>(record);
		return this.count(queryWrapper);
	}

	@Override
	public List<T> list(T record) {
		Wrapper<T> queryWrapper = new QueryWrapper<T>(record);
		return this.list(queryWrapper);
	}

	@Override
	public T getOne(T record) {
		Wrapper<T> queryWrapper = new QueryWrapper<T>(record);
		return this.getOne(queryWrapper);
	}

}
